import { createRouter, createWebHashHistory } from 'vue-router'
import Loading from '@/views/Loading.vue'
import CN from '@/views/CN/CN.vue'
import EN from '@/views/EN/EN.vue'
import CN_Index from '@/views/CN/Index.vue'
import CN_Main from '@/views/CN/Main.vue'
import CN_Map_VR from '@/views/CN/Map_VR.vue'
import CN_Map_local from '@/views/CN/Map_local.vue'
import CN_VR from '@/views/CN/VR.vue'
import CN_Video from '@/views/CN/Video.vue'
import CN_Live from '@/views/CN/Live.vue'
import EN_Index from '@/views/EN/Index.vue'
import EN_Main from '@/views/EN/Main.vue'
import EN_Map_VR from '@/views/EN/Map_VR.vue'
import EN_VR from '@/views/EN/VR.vue'
import EN_Map_local from '@/views/EN/Map_local.vue'
import EN_Live from '@/views/EN/Live.vue'
import EN_Video from '@/views/EN/Video.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'loading',
      component: Loading
    },
    {
      path: '/cn',
      component: CN,
      children: [
        {
          path: '',
          component: CN_Index
        },
        {
          path: 'main',
          component: CN_Main,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'map_vr',
          component: CN_Map_VR,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'map_local',
          component: CN_Map_local,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'video',
          component: CN_Video,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'vr',
          component: CN_VR,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'live',
          component: CN_Live,
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/en',
      component: EN,
      children: [
        {
          path: '',
          component: EN_Index,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'main',
          component: EN_Main,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'map_vr',
          component: EN_Map_VR,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'vr',
          component: EN_VR,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'map_local',
          component: EN_Map_local,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'live',
          component: EN_Live,
          meta: {
            requireAuth: true
          }
        },
        {
          path: 'video',
          component: EN_Video,
          meta: {
            requireAuth: true
          }
        }
      ]
    },
    {
      path: '/:catchAll(.*)',
      component: () => import('@/views/NotFound.vue')
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    // always scroll to top
    return { top: 0 }
  }
})

router.beforeEach((to, from, next) => {
  // 判断该路由是否需要登录权限
  if (to.meta.requireAuth) {
    // 判断当前的token是否存在
    if (localStorage.token) {
      // console.log("token存在");
      next()
    } else {
      // console.log("token不存在");
      next({
        path: '/', // 将跳转的路由path作为参数，登录成功后跳转到该路由
        query: { redirect: to.fullPath }
      })
    }
  } else {
    // 如果不需要权限校验，直接进入路由界面
    next()
  }
})

export default router
